﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QuMianChaZhi.SurfaceInterpolation
{
    public class InterpolationMethodOne : InterpolationMethod
    {
        private List<Point> mInitPoint;

        private int beta = 5;

        public double[,] CalculateInterpolation(List<Point> initPoints)
        {
            mInitPoint = initPoints;

            double[,] ret = new double[101, 101];

            for (int x = -50; x <= 50; x++)
            {
                for (int y = -50; y <= 50; y++)
                {
                    double z = CalculateValue(x, y);

                    ret[x + 50, y + 50] = z;
                }
            }

            return ret;
        }



        public double CalculateValue(double x, double y)
        {
            double z = CalculateDistance(x, y);

            if (z != -100)
            {
                return z;
            }
            CalculateWeight();

            return GetTargetZ();
        }

        private double CalculateDistance(double x, double y)
        {
            foreach (var pt in mInitPoint)
            {

                pt.d = Math.Sqrt(Math.Pow((x - pt.x), 2) + Math.Pow((y - pt.y), 2));

                if (pt.d < 0.0000001 && pt.d > -0.000001)
                {
                    return pt.z;
                }
            }

            return -100;
        }

        private double CalculateFenMu()
        {
            double fenmu = 0;
            foreach (var pt in mInitPoint)
            {
                fenmu += Math.Pow((1 / pt.d), beta);
            }

            return fenmu;
        }


        private void CalculateWeight()
        {
            double fenmu = CalculateFenMu();



            foreach (var pt in mInitPoint)
            {
                pt.w = Math.Pow((1 / pt.d), beta) / fenmu;
            }
        }

        private double GetTargetZ()
        {
            double z = 0.0;
            foreach (var pt in mInitPoint)
            {
                z += pt.z * pt.w;
            }

            return z;
        }
    }
}
